/**
 * Device路由模块
 * routes\device.js
 */
 
/**
 * @swagger
 *
 * components:
 *   schemas:
 *     Device:
 *       required:
 *         - id
 *         - title
 *         - xiamenId
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           format: int64
 *           example: 10
 *         title:
 *           type: string
 *           description: 设备名称
 *         brand:
 *           type: string
 *           description: 设备品牌
 *         imageurl:
 *           type: string
 *           description: 设备图片url
 *         spcialtype:
 *           type: string
 *           description: 设备类型
 *         status:
 *           type: string
 *           description: 状态
 *         category:
 *           type: string
 *           description: 范畴
 *         identify:
 *           type: string
 *           description: 认证
 *         price:
 *           type: string
 *           description: 价格
 *         danwei:
 *           type: string
 *           description: 所在单位
 *         contact:
 *           type: string
 *           description: 联系人
 *         phone:
 *           type: string
 *           description: 电话号码
 *         email:
 *           type: string
 *           description: email地址
 *         description:
 *           type: string
 *           description: 学科领域
 *         xiamenId:
 *           type: string
 *           description: 设备id
 *         technicalSpecs:
 *           type: string
 *           description: 设备范围
 *         mainFunctions:
 *           type: string
 *           description: 主要区域
 *         serviceContent:
 *           type: string
 *           description: 服务内容
 *         openSharingRules:
 *           type: string
 *           description: 规则
 */
 
var express = require("express");
var router = express.Router();
 
//导入所需控制器
const { fetchFromDouban } = require("../controller/fetch");
const device = require("../controller/labs");
const auth = require("../controller/auth");
//定义路由

//守卫路由：所有路由都要登录，都需要权限判断
// router.all("*",auth.isLogin,auth.isAdmin);

//TODO:增加新的功能，及增加对应的路由
/**
 * @swagger
 * /device/fetch/xiamen/{xiamenID}:
 *   get: 
 *     tags: 
 *       - 设备信息路由
 *     summary: 依据xiamenID号，从厦门大学设备获取设备信息
 *     description: 返回从厦门大学爬取的设备详细信息
 *     parameters:
 *       - in: path
 *         name: xiamenID
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     responses:
 *       200: 
 *         description: "获取成功"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/Device'
 * 
*/
router.get("/fetch/xiamen/:id", auth.isAdmin,fetchFromDouban);


/**
 * @swagger
 * /device/id/{xaimenID}:
 *   get: 
 *     tags: 
 *       - 设备路由
 *     summary: 依据ID号获取设备信息
 *     description: 返回设备详细信息
 *     parameters:
 *       - in: path
 *         name: xaimenID
 *         required: true
 *         schema:
 *           type: string
 *         description: 厦门大学的设备xiamenID号
 *     responses:
 *       200: 
 *         description: "获取成功"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/Device'
 * 
*/
router.get("/id/:id", auth.isAdmin,device.findById);

/**
 * @swagger
 * /device/search:
 *   get:
 *     tags:
 *       - 设备路由
 *     summary: 依据关键字获取设备列表信息
 *     description: 返回符合要求的所有设备列表信息
 *     parameters:
 *       - name: q
 *         in: query
 *         description: 查询关键字
 *         required: true
 *         explode: true
 *         schema:
 *           type: string
 *       - name: limit
 *         in: query
 *         description: 查询数量
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 查询偏移量
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Device'
 */
router.get("/search", device.search);
/**
 * @swagger
 * /device/count:
 *   get:
 *     tags:
 *       - 设备路由
 *     summary: 获取设备总数
 *     description: 返回设备总数
 *     responses:
 *       200:
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 */
router.get("/count", device.getCount);

/**
 * @swagger
 * /device/{id}:
 *   get:
 *     tags:
 *       - 设备路由
 *     summary: 获取指定ID设备信息
 *     description: 返回指定设备信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 设备的ID
 *     responses:
 *       200:
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/Device'
 */
router.get("/:id", device.find);
 
/**
 * @swagger
 * /device:
 *   get: 
 *     tags: 
 *       - 设备路由
 *     summary: 获取设备列表信息
 *     description: 返回所有设备列表信息
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 查询数量
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 查询偏移量
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer 
 *     responses:
 *       200: 
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Device'
*/
router.get("/", device.findAll);
 
/**
 * @swagger
 * /device:
 *   post: 
 *     tags: 
 *       - 设备路由
 *     summary: 新增设备信息
 *     description: 新增
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/Device"
 *       required: true
 *     responses:
 *       200: 
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: string
 * 
*/
router.post("/", auth.isAdmin,device.add);
 
/**
 * @swagger
 * /device/{id}:
 *   put: 
 *     tags: 
 *       - 设备路由
 *     summary: 修改设备信息
 *     description: 修改
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 设备的id 
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/Device"
 *       required: true
 *     responses:
 *       200: 
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: string
 * 
*/
router.put("/:id", auth.isAdmin,device.update);
 
/**
 * @swagger
 * /device/{id}:
 *   delete: 
 *     tags: 
 *       - 设备路由
 *     summary: 删除设备信息
 *     description: 删除
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 设备的ID 
 *     responses:
 *       200: 
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: string
 * 
*/
router.delete("/:id", auth.isAdmin,device.remove);

/**
 * @swagger
 * /device/{id}/status:
 *   put:
 *     summary: 设置设备状态（维护/停用）
 *     tags: [Device]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 设备ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: ['available', 'maintenance', 'deactivated']
 *                 description: 设备的新状态
 *     responses:
 *       200:
 *         description: 设备状态更新成功
 *       400:
 *         description: 无效的请求参数或状态
 *       401:
 *         description: 未登录或会话过期
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 *       500:
 *         description: 服务器错误
 */
router.put("/:id/status", auth.isLogin, auth.isAdmin, device.setDeviceStatus);

module.exports = router;
 